package m

import (
	log "github.com/sirupsen/logrus"

	"GameServer/Common/data"
	"GameServer/Game/cache"
	"GameServer/Game/dbmodule"
)

type UnionRank struct {
	RankId    int64     `bson:"rankId" json:"rankId"`
	UnionId   string    `bson:"unionId" json:"unionId"`
	Name      string    `bson:"name" json:"name"`
	Ico       *UnionIco `bson:"ico" json:"ico"`
	ShortName string    `bson:"shortName" json:"shortName"`
	Star      int64     `bson:"star" json:"star"`
}

type UserRank struct {
	RankId      int64  `bson:"rankId" json:"rankId"`
	Uid         string `bson:"uid" json:"uid"`
	Name        string `bson:"name" json:"name"`
	Head        string `bson:"head" json:"head"`
	Star        int64  `bson:"star" json:"star"`
	AvatarFrame int64  `bson:"avatarFrame" json:"avatarFrame"`
}

type Rank struct {
	BaseM
	UnionRanks []*UnionRank `bson:"unionRanks"`
	UserRanks  []*UserRank  `bson:"userRanks"`
}

func NewRank() cache.EntryI {
	return &Rank{
		UnionRanks: make([]*UnionRank, 0),
		UserRanks:  make([]*UserRank, 0),
	}
}

// 保存信息
func (rank *Rank) Save() (err error) {
	err = dbmodule.MgoDB.GameDB.Upsert("rank", nil, rank)
	if err != nil {
		return
	}
	return
}

func (rank *Rank) Load(key interface{}) (err error) {
	return rank.LoadData()
}

// 加载数据
func (rank *Rank) LoadData() (err error) {
	err = dbmodule.MgoDB.GameDB.FindOne("rank", nil, nil, &rank)
	if err != nil {
		if err.Error() == "not found" {
			return nil
		}
		return
	}
	return
}

func RankSort() {
	Cache.SaveTable("user")
	Cache.SaveTable("union")
	users := make([]*User, 0)
	err := dbmodule.MgoDB.GameDB.FindAllSortLimit("user", nil, nil, &users, []string{"-starSum", "starChangeTime"}, 120)
	if err != nil {
		log.Error("RankSort获取用户数据失败：", err)
		return
	}
	rank := &Rank{
		UnionRanks: make([]*UnionRank, 0),
		UserRanks:  make([]*UserRank, 0),
	}
	var avatarFrame int64 = 0
	for i, item := range users {
		avatarFrame = item.AvatarFrame
		if avatarFrame == 0 {
			avatarFrame = data.AvatarFrameEnum.DefaultFrame
		}
		rank.UserRanks = append(rank.UserRanks, &UserRank{
			RankId: int64(i + 1),
			Uid:    item.Uid.Hex(),
			Name:   item.Name,
			Head:   item.Head,
			// Star:        item.StarSum,
			Star:        item.StarVal(),
			AvatarFrame: avatarFrame,
		})
	}
	unions := make([]*Union, 0)
	err = dbmodule.MgoDB.GameDB.FindAllSortLimit("union", nil, nil, &unions, []string{"-star", "starChangeTime"}, 1020)
	if err != nil {
		log.Error("RankSort获取公会数据失败：", err)
		return
	}
	for i, item := range unions {
		rank.UnionRanks = append(rank.UnionRanks, &UnionRank{
			RankId:    int64(i + 1),
			UnionId:   item.Id.Hex(),
			Name:      item.Name,
			ShortName: item.ShortName,
			Star:      item.Star,
			Ico:       item.Ico,
		})
	}
	Cache.Put("rank", "rank", rank)
	rank.Save()
}

func GetUnionRanks() (r []*UnionRank) {
	v, _ := Cache.Get("rank", "rank")
	r = v.(*Rank).UnionRanks
	if len(r) > 1000 {
		r = r[:1000]
	}
	return
}

func GetUserRanks() (r []*UserRank) {
	v, _ := Cache.Get("rank", "rank")
	r = v.(*Rank).UserRanks
	if len(r) > 100 {
		r = r[:100]
	}
	return
}
